<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-row>
        <el-col :span="6">
          <el-form-item label="职工姓名" prop="nickName">
            <el-input
              v-model="queryParams.nickName"
              placeholder="请输入职工姓名"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="手机号" prop="phonenumber">
            <el-input
              v-model="queryParams.phonenumber"
              placeholder="请输入手机号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="帮扶类型" prop="bfLx">
            <el-select v-model="queryParams.bfLx" placeholder="请选择帮扶类型" clearable>
              <el-option
                v-for="dict in dict.type.knbf_lx"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="帮扶状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="帮扶状态" clearable>
              <el-option
                v-for="dict in dict.type.knbf_sq_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="公司" prop="deptId">
            <treeselect v-model="queryParams.deptId" style="width: 300px" :options="deptOptions"
                        :normalizer="normalizer" placeholder="选择公司"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:sq:add']"
        >新增
        </el-button>
      </el-col>-->
      <el-col :span="1.5">
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--        >修改-->
<!--        </el-button>-->
      </el-col>
      <el-col :span="1.5">
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--        >删除-->
<!--        </el-button>-->
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['system:sq:export']"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div class="infodata"  v-if="type=='2'">
      <div v-if="fxdata.companyname">机构名称：{{ fxdata.companyname }}</div>
      <div v-else></div>
      <div>帮扶申请回复总数据：{{ fxdata.totalnum }}</div>
      <div>未回复数据：{{ fxdata.noreplynum }}</div>
      <div>已回复数据：{{ fxdata.replynum }}</div>
      <div>结案率：{{ fxdata.closeLv }}%</div>
      <div>满意率：{{ fxdata.myLv }}%</div>
    </div>

    <el-table v-loading="loading" :data="sqList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="标题" align="center" prop="title"/>
      <el-table-column label="诉求内容" align="center" prop="content"/>
      <!--      <el-table-column label="用户Id" align="center" prop="userId" />-->
      <el-table-column label="帮扶人" align="center" prop="nickName"/>
      <el-table-column label="帮扶人电话" align="center" prop="phonenumber" width="120" />
      <el-table-column label="所属公司" align="center" prop="deptName"  width="200"/>
<!--      <el-table-column label="公司" align="center" prop="companyId"  width="200"/>-->
      <el-table-column label="是否保密" align="center" prop="isBm">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_no" :value="scope.row.isBm"/>
        </template>
      </el-table-column>
      <el-table-column label="是否回复" align="center" prop="isReply">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_no" :value="scope.row.isReply"/>
        </template>
      </el-table-column>
      <el-table-column label="帮扶类型" align="center" prop="bfLx">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.knbf_lx" :value="scope.row.bfLx"/>
        </template>


      </el-table-column>
      <!--      <el-table-column label="诉求类型" align="center" prop="appealLx">-->
      <!--        <template slot-scope="scope">-->
      <!--          <dict-tag :options="dict.type.knbf_appeal_type" :value="scope.row.appealLx"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="帮扶状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.knbf_sq_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="img" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="视频" align="center" prop="video" width="100" />-->
      <el-table-column label="是否需要上级处理" align="center" prop="higherCl" width="180">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_sj_cl" :value="scope.row.higherCl"/>
        </template>
      </el-table-column>
      <el-table-column label="是否满意" align="center" prop="pjStatus" width="180">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pj_status_type" :value="scope.row.pjStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="结案时间" align="center" prop="closeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.closeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" fixed="right" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >查看
          </el-button>
          <el-button v-if="type=='1'"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleReply(scope.row)"
          >回复
          </el-button>
          <el-button v-if="type=='1'"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="highterDo(scope.row)"
                     v-hasPermi="['higher:cl']"
          >需要上级处理
          </el-button>
          <el-button v-if="type=='2' &&  scope.row.status!='3'"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleJan(scope.row)"
          >结案
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改困难帮扶申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item label="帮扶人" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入用户Id"/>
        </el-form-item>
        <el-form-item label="帮扶人电话" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入帮扶人电话"/>
        </el-form-item>

        <el-form-item label="所在公司" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入所在公司"/>
        </el-form-item>
        <el-form-item label="是否保密" prop="isBm">
          <el-select v-model="form.isBm" placeholder="请选择帮扶类型">
            <el-option
              v-for="dict in dict.type.is_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否回复" prop="isReply">
          <el-select v-model="form.isReply" placeholder="请选择帮扶类型">
            <el-option
              v-for="dict in dict.type.is_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="帮扶类型" prop="bfLx">
          <el-select v-model="form.bfLx" placeholder="请选择帮扶类型">
            <el-option
              v-for="dict in dict.type.knbf_lx"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="诉求类型" prop="appealLx">-->
        <!--          <el-select v-model="form.appealLx" placeholder="请选择诉求类型">-->
        <!--            <el-option-->
        <!--              v-for="dict in dict.type.knbf_appeal_type"-->
        <!--              :key="dict.value"-->
        <!--              :label="dict.label"-->
        <!--              :value="dict.value"-->
        <!--            ></el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item label="帮扶状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择">
            <el-option
              v-for="dict in dict.type.knbf_sq_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片" prop="img">
          <image-upload v-model="form.img"/>
        </el-form-item>
<!--        <el-form-item label="视频" prop="video">-->
<!--          <file-upload v-model="form.video" :file-type="['mp4']"/>-->
<!--        </el-form-item>-->
        <el-form-item label="诉求内容">
          <el-input type="textarea" :rows="5" v-model="form.content"/>
        </el-form-item>
        <!--        <el-form-item label="结案时间" prop="closeTime">-->
        <!--          <el-date-picker clearable-->
        <!--                          v-model="form.closeTime"-->
        <!--                          type="datetime"-->
        <!--                          value-format="yyyy-MM-dd"-->
        <!--                          placeholder="请选择结案时间"-->
        <!--          >-->
        <!--          </el-date-picker>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="replyopen" width="1200px" append-to-body>
      <div class="replyContent">
        {{ form.content }}
      </div>
      <div class="replyList">
        <ReplyList :business-id="businessId" :status="status" @sxlist="sxlist"  @toCloseReply="toCloseReply" v-if="replyopen"/>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelReply">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSq, getSq, delSq, addSq, updateSq, getTjfxFwData, updateJan, updateHighter } from '@/api/business/knbfsq'
import { listDept } from '@/api/system/dept'

import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import ReplyList from '@/components/ReplyList'

export default {
  name: 'Bfsq',
  components: { ReplyList, Treeselect },
  dicts: ['knbf_appeal_type', 'knbf_lx', 'knbf_sq_status', 'is_no', 'pj_status_type','is_sj_cl'],
  props: {
    type: {
      default: 1
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 困难帮扶申请表格数据
      sqList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      replyopen: false,
      businessId: null,
      status: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        content: null,
        isBm: null,
        isReply: null,
        bfLx: null,
        appealLx: null,
        status: null,
        nickName: null,
        phonenumber: null,
        deptId: null,
        queryType: null,
      },
      fxdata: {},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: '编号不能为空', trigger: 'blur' }
        ],
        img: [
          { required: true, message: '图片不能为空', trigger: 'blur' }
        ]
      },
      // 部门树选项
      deptOptions: []
    }
  },
  created() {
    this.getList()
    this.getDeptTreeList()
    this.gefxdata()
  },
  methods: {
    gefxdata() {
      getTjfxFwData({
        deptId: this.queryParams.deptId
      }).then(response => {
        this.fxdata = response.data
        console.log(this.fxdata, 'xxxxx')
      })
    },
    toCloseReply() {
      // 1秒后关闭
      setTimeout(() => {
        this.replyopen = false
        this.getList()
      }, 1000)

    },
    sxlist() {
      // 1秒后关闭
      this.getList()
    },

    /** 查询部门列表 */
    getDeptTreeList() {
      listDept().then(response => {
        this.deptOptions = this.handleTree(response.data, 'deptId')
        console.log(this.deptOptions, 'ppppppppppp')
      })
    },

    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      }
    },
    /** 查询困难帮扶申请列表 */
    getList() {
      this.loading = true
      this.queryParams.queryType=this.type;
      listSq(this.queryParams).then(response => {
        this.sqList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    cancelReply() {
      this.replyopen = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        content: null,
        userId: null,
        isBm: null,
        isReply: null,
        bfLx: null,
        appealLx: null,
        status: null,
        img: null,
        video: null,
        closeTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        remark: null,
        nickName: null,
        phonenumber: null,
        deptName: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
      this.gefxdata()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加困难帮扶申请'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getSq(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '查看帮扶申请'
      })
    },

    handleReply(row) {
      this.reset()
      const id = row.id
      this.businessId = id
      this.status = row.status
      getSq(id).then(response => {
        this.form = response.data
        this.replyopen = true
        this.title = '回复'
      })
    },
    highterDo(row) {
      const  formdata={
        businessId:row.id,
      }
      this.$modal.confirm('确定需要上级处理吗？').then(function() {
        return updateHighter(formdata);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("成功");
        // 结案 给父组件通知 关闭 弹窗
        this.$emit('toCloseReply');
      }).catch(() => {});
    },
    handleJan(row){
      const  formdata={
        businessId:row.id,
      }
      this.$modal.confirm('确定结案吗？').then(function() {
        return updateJan(formdata);
      }).then(() => {
        this.getList();
        this.gefxdata()
        this.$modal.msgSuccess("成功");
        // 结案 给父组件通知 关闭 弹窗
        this.$emit('toCloseReply');
      }).catch(() => {});

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSq(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addSq(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除困难帮扶申请编号为"' + ids + '"的数据项？').then(function() {
        return delSq(ids)
      }).then(() => {
        this.getList()
        this.gefxdata()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sq/export', {
        ...this.queryParams
      }, `sq_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style scoped>
.replyContent {
  min-height: 100px;
  overflow-y: auto;
  background-color: #F2F2F2;
  /*border: 1px solid #dcdfe6;*/
  padding: 5px;
}

.infodata {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 80px;
  border: 1px solid #dcdfe6;
  padding: 20px;
  margin-bottom: 10px;


}


</style>

